import { myNotifyMock } from './data/my'
import { isDateTimeInRange, getNowDateTime } from '../../../utils'
let myNotifyList = myNotifyMock(400)
export default [
  {
    url: RegExp('/system/notify-record/my-page' + '.*'), // 查询分页列表
    method: 'get',
    response: params => {
      return {
        code: 200,
        status: true,
        msg: '成功！',
        data: getListByPage(params)
      }
    }
  },
  {
    url: RegExp('/system/notify-record/update-read' + '.*'), // 批量标记已读
    method: 'put',
    response: params => {
      updateRead(params)
      return {
        code: 200,
        status: true,
        msg: '成功！',
        data: null
      }
    }
  },
  {
    url: RegExp('/system/notify-record/update-all-read' + '.*'), // 标记所有已读
    method: 'put',
    response: () => {
      updateAllRead()
      return {
        code: 200,
        status: true,
        msg: '成功！',
        data: null
      }
    }
  },
  {
    url: RegExp('/system/notify-record/get-unread-list' + '.*'), // 获取当前用户未读站内信
    method: 'get',
    response: () => {
      return {
        code: 200,
        status: true,
        msg: '成功！',
        data: getListByUnread()
      }
    }
  },
  {
    url: RegExp('/system/notify-record/get-unread-count' + '.*'), // 获取当前用户未读站内信数量
    method: 'get',
    response: () => {
      return {
        code: 200,
        status: true,
        msg: '成功！',
        data: getCountByUnread()
      }
    }
  }
]

const getListByPage = params => {
  let result = myNotifyList
  result = result.filter(item => !item.deleted)
  if (params.query.readStatus !== undefined) {
    result = result.filter(item => item.readStatus + '' === params.query.readStatus)
  }
  if (params.query.create_time && params.query.create_time.length !== 0) {
    result = result.filter(item => isDateTimeInRange(params.query.create_time[0], params.query.create_time[1], item.create_time))
  }
  return {
    list: result.slice(parseInt(params.query.pageSize) * (parseInt(params.query.pageIndex) - 1), parseInt(params.query.pageSize) * (parseInt(params.query.pageIndex) - 1) + parseInt(params.query.pageSize)),
    total: result.length
  }
}

const updateRead = params => {
  console.log(params.query.ids)
  if (Array.isArray(params.query.ids)) {
    params.query.ids.forEach(v => {
      myNotifyList.forEach(item => {
        if (item.id === parseInt(v)) {
          item.readStatus = true
          item.readTime = getNowDateTime()
          item.updater = '1'
          item.update_time = getNowDateTime()
        }
      })
    })
  } else {
    myNotifyList.forEach(item => {
      if (item.id === parseInt(params.query.ids)) {
        item.readStatus = true
        item.readTime = getNowDateTime()
        item.updater = '1'
        item.update_time = getNowDateTime()
      }
    })
  }
}

const updateAllRead = () => {
  myNotifyList.forEach(item => {
    if (!item.readStatus) {
      item.readStatus = true
      item.readTime = getNowDateTime()
      item.updater = '1'
      item.update_time = getNowDateTime()
    }
  })
}
const getListByUnread = () => {
  let result = myNotifyList
  return result.filter(item => item.readStatus === false)
}
const getCountByUnread = () => {
  let result = myNotifyList
  return result.filter(item => item.readStatus === false).length
}
